﻿using System;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace Dynamic.Data.Model.Query
{
    [DataContract]
    [XmlType(Namespace = "DynamicMatrix")]
    public sealed class ConditionExpression
    {
        private string _entityName;
        private string _attributeName;
        private ConditionOperator _conditionOperator;
        private object[] _values;

        public ConditionExpression()
        {
        }

        public ConditionExpression(string entityName, string attributeName, ConditionOperator conditionOperator)
            : this(entityName, attributeName, conditionOperator, (Array)new object[0])
        {
        }

        public ConditionExpression(string entityName, string attributeName, ConditionOperator conditionOperator, object value)
            : this(entityName, attributeName, conditionOperator, (Array)new object[] { value })
        {
        }

        public ConditionExpression(string entityName, string attributeName, ConditionOperator conditionOperator, Array values)
        {
            this._entityName = entityName;
            this._attributeName = attributeName;
            this._conditionOperator = conditionOperator;
           
            if (values != null)
            {
                this._values = new object[values.Length];
                for (int i = 0; i < values.Length; i++)
                {
                    this._values[i] = values.GetValue(i);
                }
            }
        }

        public void Accept(IQueryVisitor visitor)
        {
            visitor.Visit(this);
        }

        [DataMember]
        public string EntityName
        {
            get
            {
                return this._entityName;
            }
            set
            {
                this._entityName = value;
            }
        }

        [DataMember]
        public string AttributeName
        {
            get
            {
                return this._attributeName;
            }
            set
            {
                this._attributeName = value;
            }
        }

        [DataMember]
        public ConditionOperator Operator
        {
            get
            {
                return this._conditionOperator;
            }
            set
            {
                this._conditionOperator = value;
            }
        }

        [DataMember]
        [XmlArray, XmlArrayItem(ElementName = "Value", IsNullable = true, Type = typeof(object))]
        public object[] Values
        {
            get
            {
                return this._values;
            }
            set
            {
                this._values = value;
            }
        }
    }
}

